﻿using MVCManage.Api.Models;
using MVCManage.BLL;
using MVCManage.Model;
using MVCManage.Model.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace MVCManage.Api.Controllers
{
    public class HJQ_store_infoController : ApiController
    {

        HJQ_store_infoBLL bll = new HJQ_store_infoBLL();

        // GET: api/xinxi
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">条数</param>
        /// <param name="name">模糊查询</param>
        /// <returns></returns>
        [ApiActionMonitor]
        public ResultPageList<HJQ_store_infoDto> Get(int page, int limit, string name = "")
        {
            int count = 0;
            //int num = int.Parse("abc");

            var list = bll.GetUsersByPage(page, limit, name, out count);

            var slist = from u in list
                        select new HJQ_store_infoDto
                        {
                            AddressID = u.AddressID,
                            shopName = u.shopName,
                            shopAddress = u.shopAddress,
                            phone = u.phone,
                            yingyeTime = u.yingyeTime,
                            jingyingfw = u.jingyingfw,
                            fuzer = u.fuzer,
                            dianpumj = decimal.Parse(u.dianpumj.ToString()),
                            shopDate = DateTime.Parse(u.shopDate.ToString()),
                            email = u.email,
                            region = u.region,
                            liansuo = u.liansuo,
                            wangAddress = u.wangAddress,
                            kefuPhone = u.kefuPhone,
                        };

            ResultPageList<HJQ_store_infoDto> res = new ResultPageList<HJQ_store_infoDto>();
            res.code = 0;
            res.msg = "okok";
            res.count = count;
            res.data = slist.ToList();

            return res;
        }


        // POST: api/xinxi
        public Result Post([FromBody] HJQ_store_infoDto md)
        {
            HJQ_store_info yy = new   HJQ_store_info();
            yy. AddressID = md.AddressID;
            yy.shopName = md.shopName;
            yy.shopAddress = md.shopAddress;
            yy.phone = md.phone;
            yy.yingyeTime = md.yingyeTime;
            yy.jingyingfw = md.jingyingfw;
            yy.fuzer = md.fuzer;
            yy.dianpumj = md.dianpumj;
            yy.shopDate = md.shopDate;
            yy.email = md.email;
            yy.region = md.region;
            yy.liansuo = bool.Parse(md.liansuo.ToString());
            yy.wangAddress = md.wangAddress;
            yy.kefuPhone = md.kefuPhone;
            bool num = bll.Add(yy);

            Result res = new Result();
            res.code = num ? 1 : 0;
            res.msg = num ? "添加成功" : "添加失败";
            return res;
        }


        // PUT: api/xinxi/5
        [System.Web.Http.Route("Api/HJQ_store_info/up")]
        public Result Put([FromBody] HJQ_store_infoDto  md)
        {
            var yy = bll.Query().Where(u => u.AddressID == md.AddressID).FirstOrDefault();
            Result res = new Result();
            if (yy != null)
            {
                yy. AddressID = md.AddressID;
                yy.shopName = md.shopName;
                yy.shopAddress = md.shopAddress;
                yy.phone = md.phone;
                yy.yingyeTime = md.yingyeTime;
                yy.jingyingfw = md.jingyingfw;
                yy.fuzer = md.fuzer;
                yy.dianpumj = md.dianpumj;
                yy.shopDate = md.shopDate;
                yy.email = md.email;
                yy.region = md.region;
                yy.liansuo = bool.Parse(md.liansuo.ToString());
                yy.wangAddress = md.wangAddress;
                yy.kefuPhone = md.kefuPhone;



                bool num = bll.Update(yy);
                res.code = num ? 1 : 0;
                res.msg = num ? "修改成功" : "修改失败";
            }
            else
            {
                res.code = 0;
                res.msg = "未找到该对象";
            }

            return res;
        }


        // DELETE: api/xinxi/5
        public Result Delete(int id)
        {
           var obj= bll.Query().Where(a=>a.AddressID==id).FirstOrDefault();
            bool num = bll.Delete(obj);
            Result res = new Result();
            res.code = num ? 1 : 0;
            res.msg = num ? "删除成功" : "删除失败";
            return res;
        }

        [System.Web.Http.Route("api/HJQ_store_info/batch")]
        public Result Delete(int[]ids)
        {
            var obj = bll.Query().Where(a => ids.Contains(a.AddressID)).ToList();
            bool num = bll.Delete(obj);
            Result res = new Result();
            res.code = num ? 1 : 0;
            res.msg = num ? "删除成功" : "删除失败";
            return res;
        }

    }
}
